package com.nuotai.pms.shiro.controller;

import com.alibaba.fastjson.JSONObject;
import com.nuotai.pms.common.constant.SystemConstant;
import com.nuotai.pms.common.controller.AbstractController;
import com.nuotai.pms.common.entity.Page;
import com.nuotai.pms.common.entity.SysUserEntity;
import com.nuotai.pms.common.entity.TaskEntity;
import com.nuotai.pms.common.utils.ShiroUtils;
import com.nuotai.pms.shiro.service.TaskService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 任务管理
 * @author yyf
 */
@RestController
@RequestMapping("/pms/task")
public class taskController extends AbstractController {
	
	@Autowired
	private TaskService taskService;
	
	/**
	 * 任务列表
	 * @param taskMap
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public String list(@RequestParam Map<String, Object> taskMap) {
		JSONObject jsonObject = new JSONObject();
		if(getUserId() != SystemConstant.SUPER_ADMIN) {
			taskMap.put("userIdCreate", getUserId());
		}
		List<TaskEntity> taskList = taskService.queryTaskList(taskMap);
		if(null != taskList){
			jsonObject.put("success","true");
			jsonObject.put("msg","任务数据获取成功!");
			jsonObject.put("taskList",taskList);
		}else {
			jsonObject.put("error","error");
			jsonObject.put("msg","任务数据获取失败，请刷新重试!");
		}
		return jsonObject.toString();
	}

	//添加任务
	@RequestMapping("/addTask")
	@ResponseBody
	public String addTask(@RequestParam Map<String,Object> addMap){
		JSONObject jsonObject = new JSONObject();
		int addTaskCount = taskService.addTask(addMap);
		if(addTaskCount >0){
			jsonObject.put("success","true");
			jsonObject.put("msg","任务数据获取成功!");
		}else {
			jsonObject.put("error","error");
			jsonObject.put("msg","任务数据获取失败，请刷新重试!");
		}
		return jsonObject.toString();
	}

	//上传票据
	/*@RequestMapping(value="/uploadTaskFile",method = RequestMethod.POST)
	@ResponseBody
	public String uploadTaskFile(@RequestParam(value = "file[]",required = false)
								 MultipartFile[] files)throws Exception{
		JSONObject json = new JSONObject();
		try {
			List urlList = new ArrayList();
			for (MultipartFile file:files){
				//获取文件名
				String fileName = file.getOriginalFilename();
				json.put("fileName",fileName);
				//获取文件后缀
				String fileExtensionName = FilenameUtils.getExtension(fileName);
				if(!fileExtensionName.equalsIgnoreCase("JPG")&&
						!fileExtensionName.equalsIgnoreCase("GIF") &&
						!fileExtensionName.equalsIgnoreCase("PNG") &&
						!fileExtensionName.equalsIgnoreCase("JPEG")){
					json.put("result", "fail");
					json.put("msg", "上传文件格式不正确！");
					return json.toString();
				}
				//获取文件大小
				String fileSize = ShiroUtils.convertFileSize(file.getSize());

			}
		}catch (Exception e){
			e.printStackTrace();
			json.put("result","fail");
			json.put("msg","上传失败！请联系管理员！");
			return json.toString();
		}
		return json.toString();
	}*/
}
